<?php
namespace app\common\model;

use think\model\concern\SoftDelete;

class Category extends Model {
    use SoftDelete;

    protected $name = 'category';
    protected $autoWriteTimestamp = true;

    //可搜索字段
    protected $searchField = ['name', 'description'];

    // 属性获取器
    //是否启用获取器
    public function getStatusTextAttr($value, $data): string
    {
        return self::BOOLEAN_TEXT[$data['status']];
    }

    // 获取分类信息
    public static function getCategoryInfo($categoryId): array
    {
        return self::field('id, name, description, status, sort')
            ->findOrEmpty($categoryId)
            ->toArray();
    }

    //获取可用的分类，并按照sort排序
    public static function getAvailableCategory(): array
    {
        return self::where('status', 1)
            ->order('sort', 'asc')
            ->select()
            ->toArray();
    }

    //获取分类列表
    public static function getCategoryList($param): array
    {
        $query = self::where([]);
        $query->scopeWhere($param);
        $query->order('sort', 'asc');
        return $query->paginate($param['per_page'] ?? 10);
    }
}